package com.kelvin.oocl.transactionBlock.v2.dao.impl;

import com.kelvin.oocl.testTransaction.exception.DBOperationException;
import com.kelvin.oocl.testTransaction.model.Customer;
import com.kelvin.oocl.testTransaction.tool.DBUtils;
import com.kelvin.oocl.transactionBlock.v2.dao.CustomerDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CustomerDaoImpl implements CustomerDao {
    @Override
    public int save(Customer customer) {
        Connection connection = null;
        PreparedStatement ps = null;
        connection = DBUtils.getConnection();
        String sql = "insert into customer(id,username,password) values(?,?,?)";
        try {
            ps = connection.prepareStatement(sql);
            ps.setInt(1, customer.getId());
            ps.setString(2, customer.getUsername());
            ps.setString(3, customer.getPassword());
            return ps.executeUpdate();
        } catch (SQLException e) {
            throw new DBOperationException("CustomerDao operation failed.");
        }
    }
}
